package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.MessageTopThirdEntity;
import com.hctforgreen.greenservice.ui.widget.MessageTopThirdPopupView;
import com.hctforgreen.greenservice.ui.widget.TerminalDataTimePopupView;

public class TerminalDataTimeListAdapter extends BaseAdapter implements
		OnItemClickListener {
	private List<MessageTopThirdEntity> mEntities;
	private Activity mActivity;
	private ListView mFirstLv;
	private MessageTopThirdEntity mCurrentFirstEntity;
	
	private TerminalDataTimePopupView mListPopupView;
	
	/**
	 * 
	 * @param firstLv
	 * @param activity
	 * @param entities---选框显示的时间选项实体类
	 * @param currentFirstEntity-----资料大类别
	 * @param listPopupView
	 */
	public TerminalDataTimeListAdapter(ListView firstLv, 
			Activity activity, List<MessageTopThirdEntity> entities,
			MessageTopThirdEntity currentFirstEntity, TerminalDataTimePopupView listPopupView) {
		mActivity = activity;
		
		mEntities = entities;
		mFirstLv = firstLv;
		
		mCurrentFirstEntity = currentFirstEntity;
		initmEntitiesSelected();
		
		mListPopupView = listPopupView;
		mFirstLv.setAdapter(this);
		mFirstLv.setOnItemClickListener(this);
		initSelection(currentFirstEntity);
	}
	/**
	 * 根据btn显示的内容来指定下拉框listview选中的项
	 * @param currentFirstEntity
	 */
	private void initSelection(MessageTopThirdEntity currentFirstEntity) {
		if(currentFirstEntity == null){
			return;
		}
		for (int i = 0; i < mEntities.size(); i++) {
			MessageTopThirdEntity entity = mEntities.get(i);
			if (entity.dateFrom.equals(currentFirstEntity.dateFrom)) {
				mFirstLv.setSelection(i);
				entity.isSelected = true;
			}
		}

	}
	
	private void initmEntitiesSelected() {
		if(mCurrentFirstEntity == null){
			return;
		}
		if (mCurrentFirstEntity.isSelected) {
			for (MessageTopThirdEntity entity : mEntities) {
				if (entity.dateFrom.equals(mCurrentFirstEntity.dateFrom)) {
					//Log.i("DATA", "currentFirstEntity.dateFrom="+mCurrentFirstEntity.dateFrom);
					entity.isSelected = true;
				}
			}
		}
	}
	@Override
	public int getCount() {
		return mEntities.size();
	}

	@Override
	public Object getItem(int position) {
		return mEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		if (convertView == null) {
			convertView = View.inflate(mActivity,
					R.layout.item_series_books_first_lst, null);
			viewCache = new ViewCache(convertView);
			convertView.setTag(viewCache);

		} else {
			viewCache = (ViewCache) convertView.getTag();
		}

		final MessageTopThirdEntity entity = (MessageTopThirdEntity) getItem(position);

		viewCache.getItemNameTv().setText(entity.name);
		if (entity.isSelected) {
			viewCache.getSelectorLyt().setBackgroundResource(
					R.drawable.ic_first_popup_item_selected_bg);
		} else {
			viewCache.getSelectorLyt().setBackgroundDrawable(null);
		}
		return convertView;
	}

	class ViewCache {
		private View baseView;

		private LinearLayout selectorLyt;
		private TextView itemNameTv;

		public ViewCache(View view) {
			baseView = view;
		}

		public LinearLayout getSelectorLyt() {
			if (selectorLyt == null) {
				selectorLyt = (LinearLayout) baseView
						.findViewById(R.id.lyt_selector);
			}
			return selectorLyt;
		}

		public TextView getItemNameTv() {
			if (itemNameTv == null) {
				itemNameTv = (TextView) baseView
						.findViewById(R.id.tv_item_name);
			}
			return itemNameTv;
		}
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		final MessageTopThirdEntity currentEntity = (MessageTopThirdEntity) getItem(position);

		clearmEntitiesSelected(currentEntity);
		currentEntity.isSelected = true;

		
		/**
		 * 通知getView()刷新
		 */
		notifyDataSetChanged();
		renderDataLst(currentEntity);
	}
	private void clearmEntitiesSelected(final MessageTopThirdEntity currentEntity) {
		for (MessageTopThirdEntity entity : mEntities) {
			if (entity.dateFrom != currentEntity.dateFrom && entity.isSelected) {
				entity.isSelected = false;
			}
		}
	}
	private void renderDataLst(MessageTopThirdEntity currentFirstEntity) {
		
		if (currentFirstEntity != null) {
			mListPopupView.updateWindow(currentFirstEntity);
			mListPopupView.dismiss();
		}

	}

	
}
